package zkk.com.mengqu.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import zkk.com.mengqu.R;
import zkk.com.mengqu.manager.APIManager;
import zkk.com.mengqu.models.KitchenDetail;
import zkk.com.mengqu.simplepreferences.Share;

/**
 * Created by Administrator on 2017/6/9.
 */
public class AddKitchenActivity extends Activity implements View.OnClickListener {
    private Context context;
    private ImageView back;
    //厨房名字、厨房类型、厨房地址、厨房服务、服务价格
    private EditText etKitchenName,etKitchenType,etKitchenAddress,etKitchenService,etKitchenPrice;
    private Button addKitchen;//注册厨房
    private LinearLayout photos;//
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_kitchen);
        initView();
    }

    public void initView(){
        context = getApplicationContext();
        back = (ImageView)findViewById(R.id.iv_back);
        addKitchen = (Button)findViewById(R.id.btn_add_kitchen);
        etKitchenName = (EditText)findViewById(R.id.et_kitchen_name);
        etKitchenType = (EditText)findViewById(R.id.et_kitchen_type);
        etKitchenAddress = (EditText)findViewById(R.id.et_kitchen_address);
        etKitchenService = (EditText)findViewById(R.id.et_kitchen_service);
        etKitchenPrice = (EditText)findViewById(R.id.et_kitchen_price);

        back.setOnClickListener(this);
        addKitchen.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_add_kitchen:

                newKitchen();
                break;
        }
    }

    public void newKitchen(){
        String name = etKitchenName.getText().toString().trim();
        String type = etKitchenType.getText().toString().trim();
        String address = etKitchenAddress.getText().toString().trim();
        String service = etKitchenService.getText().toString().trim();
        String price = etKitchenPrice.getText().toString().trim();
        String uid = Share.getUserId();
        String token = Share.getUserToken();
        Log.w("AddKitchen",uid+","+token+","+name+","+type+","+address+","+service+","+price);
        APIManager.setManager()
                .newKitchen("new_kitchen" , uid,token,name,"113.584945","22.28563",
                                            type,address,service,price,"","")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<KitchenDetail>() {
                    @Override
                    public void onCompleted() {
                        Log.w("onCompleted","onCompleted");
                        finish();
                        Toast.makeText(context , "注册厨房成功",Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.w("onError","onError");
                        e.printStackTrace();

                    }

                    @Override
                    public void onNext(KitchenDetail kitchenDetail) {
                        Log.w("onNext","onNext");
                    }
                });
    }
}
